package com.bootdo.act.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.act.domain.ActTeamDO;
import com.bootdo.act.service.ActTeamService;
import com.bootdo.act.service.TeamUserService;
import com.bootdo.common.controller.BaseController;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 队伍表
 * 
 * @author kwx
 * @email 904358838@qq.com
 * @date 2017-12-18 16:05:55
 */
 
@Controller
@RequestMapping("/act/actTeam")
public class ActTeamController extends BaseController{
	@Autowired
	private ActTeamService actTeamService;
	
	@Autowired
	private TeamUserService teamUserService;
	
	@GetMapping()
	@RequiresPermissions("act:actTeam:actTeam")
	String ActTeam(){
	    return "act/actTeam/actTeam";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("act:actTeam:actTeam")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<ActTeamDO> actTeamList = actTeamService.list(query);
		int total = actTeamService.count(query);
		PageUtils pageUtils = new PageUtils(actTeamList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("act:actTeam:add")
	String add(Model model){
		model.addAttribute("createrId", getUserId());
		model.addAttribute("createrName", getUsername());
	    return "act/actTeam/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("act:actTeam:edit")
	String edit(@PathVariable("id") Long id,Model model){
		ActTeamDO actTeam = actTeamService.get(id);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("teamId", id);
		actTeam.setUserList(teamUserService.list(map));
		model.addAttribute("actTeam", actTeam);
	    return "act/actTeam/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("act:actTeam:add")
	public R save( ActTeamDO actTeam){
		actTeam.setCreateDate(new Date());
		actTeam.setCreaterId(getUserId());
		actTeam.setCreaterName(getUsername());
		if(actTeamService.save(actTeam)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("act:actTeam:edit")
	public R update( ActTeamDO actTeam){
		actTeam.setUpdateDate(new Date());
		actTeamService.update(actTeam);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("act:actTeam:remove")
	public R remove( Long id){
		if(actTeamService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("act:actTeam:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids){
		actTeamService.batchRemove(ids);
		return R.ok();
	}
	
}
